<?php 
namespace app\admin\controller;

use think\Db;

class Message extends Base
{

    /* 留言管理 */
    public function messagelist(){
        $input = input('param.');
        $list = Db::name('feedback')->order('id','desc')->paginate(10);
        $count = Db::name('feedback')->count();
        $this->assign('list',$list);
        $this->assign('count',$count);
        $this->assign('page',$list->render());
        return $this->fetch();
    }

    /* 留言回复 */
    public function message_add(){
        $data = input('param.');
        if($this->request->isPost()){
        	$data['reply_time']=time();
            $b = Db::name('feedback')->where(['id'=>$data['id']])->update($data);
            if($b){
                return json(['status'=>'y','info'=>'回复成功']);
            }else{
                return ['status'=>'n','info'=>'操作失败'];
            }
        }else{
            $this->assign('id',$data['id']);
            return $this->fetch();
        }
        
    }

    public function news_list(){
        $db = Db::name('introduction');
        $where = array();
        $word = input('word');
        $time_min = input('time_min');
        $time_max = input('time_max');
        
        if(!empty($time_max) && !empty($time_min)){
            $time_max = strtotime($time_max);
            $time_min = strtotime($time_min);
            if($time_max >= $time_min){
                $where['add_time'] = array('between',array($time_min,$time_max));
            }else{
                $where['add_time'] = array('between',array($time_max,$time_min));
            }
        }elseif(!empty($time_min)){
            $time_min = strtotime($time_min);
            $time_max = !empty($time_max) ? strtotime($time_max) : time()+999999999;
            $where['add_time'] = array('between',array($time_min,$time_max));
        }elseif(!empty($time_max)){
            $time_max = strtotime($time_max);
            $where['add_time'] = array('elt',$time_max);
        }
        $count = $db->where($where)->count();

        $list = $db->field('id,title,desc,type,add_time')->whereLike('title','%'.$word.'%')->where($where)->order('id desc')->paginate(5);
        $this->assign('list',$list);
        $this->assign('page',$list->render());
        $this->assign('count',$count);
        return $this->fetch();
    }


    /*添加新闻*/
    public function news_add(){
        $db = Db::name('introduction');
        if($this->request->isPost()){
            $data = input('post.');
            foreach ($data as $k => $v) {
            	if(empty($v) && $k !='type' && $k !='id'){
                    return json(['status'=>'n','info'=>'资料不能为空!']);
            	}
            }
            $data['add_time'] = time();
            if($_POST['id'] > 0){
                $b = $db->update($data);
            }else{
                $b = $db->insert($data);
            }
            if($b){
                return json(['status'=>'y','info'=>'操作成功']);
            }else{
                return json(['status'=>'n','info'=>'操作失败!']);
            }
        }else{
            $id = input('id',0);
            if($id > 0){
                $row = $db->find($id);
                $row['add_time'] = time();
            }else{
                $row = array('publish_time'=>date('Y-m-d'));
            }
        }
        $this->assign($row);
        return $this->fetch();
    }

    public function news_del(){
        $id = trim(input('id'),',');
        $ids = explode(',',$id);
        $db = Db::name('introduction');
        //查询缩略图和内容
        $b = $db->where('id','in',$ids)->delete();
        $count = $db->count();
        if($b){
            return json(['status'=>'y','info'=>'删除成功','count'=>$count]);
        }else{
            return json(['status'=>'n','info'=>'删除失败!']);
        }
    }

}